﻿//创建时间: 2010-10-11
//创建作者: 李 城
//功能说明: 内容模型【字段】  列出所有字段 
//最后修改: 2010-10-11

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;

namespace TRPOP.CMS.Web.Admin
{
    public partial class Model_save : BaseAdmin
    {

        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.SystemManage_ContentModel;
        }
        #endregion

        #region 页面变量
        protected string m_strTitle = "模型管理";
        ModelInfo m_ModelInfo = new ModelInfo();
        ModelManage m_ModelManage = new ModelManage();
        ModelFieldManage m_ModelFieldManage = new ModelFieldManage();
        protected int m_id = 0;
        #endregion

        #region 页面事件

        protected void Page_Load(object sender, EventArgs e)
        {
            base.AlertForNoPermissionControl(TRPOP.CMS.Plugin.VersionLimit.ContentModelEdit);
            if (!IsPostBack)
            {
                LoadBase();
                BindData();
            }
        }

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            LinkButton lbtn = (LinkButton)sender;
            int intDelId = int.Parse(lbtn.CommandArgument);
            SubmitAlertDelete(m_ModelFieldManage.DeleteInfoById(intDelId));
            BindData();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strMSG = "模型名称";
            if (!PageAssist.CheckForm(txtModelName, ref strMSG)
                )
            {
                Alert(strMSG);
                return;
            }
            m_ModelInfo = (ModelInfo)GetPageViewState("model");
            if (m_ModelInfo == null)
            {
                Alert("请通过正确的途径进入本页面");
                return;
            }
            else
            {
                m_ModelInfo.Name = txtModelName.TextFilterSQL.RemoveHtml();
               // m_ModelInfo.Description = Utils.RemoveHtml(txtDescription.TextFilterSQL);
                base.SaveLog(Plugin.LogKeys.OperObject.ModelOper, Plugin.LogKeys.OperResult.EditSubmit, RequestHelper.RequestInt("m_id").ToString());
                SubmitAlert(m_ModelManage.Save(m_ModelInfo));
            }
            BindData();

        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ModelFieldInfo oModelFieldInfo = ((ModelFieldInfo)e.Item.DataItem);
                if (oModelFieldInfo.IsMainField)
                {
                    ((Literal)e.Item.FindControl("ltMainField")).Visible = true;
                    ((LinkButton)e.Item.FindControl("LbtMainField")).Visible = false;
                }
                if (oModelFieldInfo.IsTitleField)
                {
                    ((Literal)e.Item.FindControl("ltTitle")).Visible = true;
                    ((LinkButton)e.Item.FindControl("LbtTitle")).Visible = false;
                }
                ((CheckBox)e.Item.FindControl("chkFieldListDisplay")).Checked = oModelFieldInfo.FieldListDisplay;
                ((CheckBox)e.Item.FindControl("chkIsSearch")).Checked = oModelFieldInfo.IsSearch;
                ((CheckBox)e.Item.FindControl("chkIsContribute")).Checked = oModelFieldInfo.IsContribute;
                ((CheckBox)e.Item.FindControl("chkIsCollect")).Checked = oModelFieldInfo.IsCollect;
                ((CheckBox)e.Item.FindControl("chkIsSearch")).Enabled = TableFieldInfo.IsSearch(oModelFieldInfo.FieldTypeStr);
                ((CheckBox)e.Item.FindControl("chkIsEdit")).Checked = oModelFieldInfo.IsEdit;

            }
        }

        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Title":
                    m_ModelFieldManage.UpdateTitleFiled(e.CommandArgument.ToString().ToInt());
                    break;
                case "Field":
                    m_ModelFieldManage.UpdateMainField(e.CommandArgument.ToString().ToInt());
                    break;
                case "FieldListDisplay":
                    m_ModelFieldManage.UpdateField(e.CommandArgument.ToString().ToInt(), "FieldListDisplay");
                    break;
                case "IsSearch":
                    m_ModelFieldManage.UpdateField(e.CommandArgument.ToString().ToInt(), "IsSearch");
                    break;
                case "IsContribute":
                    m_ModelFieldManage.UpdateField(e.CommandArgument.ToString().ToInt(), "IsContribute");
                    break;
                case "IsCollect":
                    m_ModelFieldManage.UpdateField(e.CommandArgument.ToString().ToInt(), "IsCollect");
                    break;
                case "IsEdit":
                    m_ModelFieldManage.UpdateField(e.CommandArgument.ToString().ToInt(), "IsEdit");
                    break;
            }
            base.SaveLog(Plugin.LogKeys.OperObject.ModelOper, Plugin.LogKeys.OperResult.ModelFieldEdit, RequestHelper.RequestInt("m_id") + ":" + e.CommandArgument.ToString());
            BindData();
        }
        protected void test_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        #endregion

        #region 页面方法

        public void LoadBase()
        {
            int.TryParse(InitRequestViewState("id"), out m_id);
            if(m_id<=0)
            {
                Alert("请通过正确的途径进入本页面");
            }
        }

        public void BindData()
        {
            int.TryParse(GetRequestViewState("id"), out m_id);
            m_ModelInfo = m_ModelManage.GetInfoById(m_id);
            if (m_ModelInfo == null)
            {
                Alert("请通过正确的途径进入本页面");
                return;
            }
            InitPageViewState("model", m_ModelInfo);
            m_strTitle = "" + m_ModelInfo.Name + " 模型";
            txtModelName.Text = m_ModelInfo.Name;
          //  txtDescription.Text = m_ModelInfo.Description;
            rptList.DataSource = m_ModelFieldManage.GetListByCondition(0, " and ModelId=" + m_id.ToString() + " ", "DisplayOrder,ModelFieldId");
            rptList.DataBind();
        }

        #endregion

    }
}
